
import { Application } from 'egg';
import { MEDIUMINT, STRING, INTEGER, TEXT, Instance } from 'sequelize';

export interface IUserAttr {
  id?: number;
  username: string;
  password: string;
  email?: string;
  avatar?: string;
  gender?: number;
  signature?: string;
  website?: string;
  status?: number;
}

export interface IUserInst extends Instance<IUserAttr>, IUserAttr { 
  id: number;
  username: string;
  password: string;
  email: string;
  avatar: string;
  gender: number;
  signature: string;
  website: string;
  status: number;
}

// <IUserInst, IUserAttr>
export default (app: Application) => {
  const user = app.model.define<IUserInst, IUserAttr>('user', {
    id: {
      type: MEDIUMINT(8).UNSIGNED,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
    },
    username: {
      type: STRING(60),
      allowNull: false,
      unique: true,
    },
    password: {
      type: STRING,
      allowNull: false,
      defaultValue: '',
    },
    email: {
      type: STRING(32),
      allowNull: false,
      defaultValue: '',
    },
    avatar: {
      type: STRING,
      allowNull: false,
      defaultValue: '',
    },
    gender: {        // 性别
      type: INTEGER(10),
      defaultValue: 3,          // 1 女 2男 3未知
    },
    signature: {     // 签名（简介）
      type: TEXT,
      defaultValue: '',
    },
    website: {       // 个人网站
      type: STRING,
      defaultValue: '',
    },
    status: {
      type: INTEGER(10),
      allowNull: false,
      defaultValue: 1,
    },
  }, {
    timestamps: true,
    paranoid: true,
    charset: 'utf8mb4',
    defaultScope: {
      where: {
        status: 1,
      },
    },
    scopes: {
      deleted: {
        where: {
          status: 0,
        },
      },
    },
  });
  // user.sync({ alter: true });
  //   user.sync({force: true});
  return user;
};
